﻿using System.Linq;
using System.Web.Mvc;
using JStore.Core.Entities;
using JStore.Tasks;
using JStore.Web.Controllers.SysMsgs.ViewModels;
using SpeadFrm.Mappers;
using SpeadFrm.MvcPager;

namespace JStore.Web.Controllers.SysMsgs
{
    public class SysMsgsController : BaseController
    {
        private readonly SysMsgTask _sysMsgTask = new SysMsgTask();

        public ActionResult Index(int pageIndex = 1, int pageSize = 10)
        {
            ViewData["MsgCount"] = _sysMsgTask.GetCount(LogOnUserName);
            var pagedList = _sysMsgTask.GetPagedList(LogOnUserName, pageIndex, pageSize);
            var items = pagedList.Select(EntityMapper.Map<SysMsg, SysMsgModel>).ToList();
            var model = new PagedList<SysMsgModel>(items, pageIndex, pageSize, pagedList.TotalItemCount);
            return View(model);
        }

        public ActionResult Detail(int id)
        {
            var info = _sysMsgTask.GetById(id);
            if (info.UserName.ToLower() != LogOnUserName.ToLower())
                return Content("对不起，您无权查看");

            _sysMsgTask.SetRead(id);
            var model = EntityMapper.Map<SysMsg, SysMsgModel>(info);
            return View(model);
        }
    }

}